﻿CREATE procedure dbo.SaveMenu
(
		@CategoryID int,
		@Name varchar(500),
		@Address varchar(255) = null,
		@City varchar(255) = null,
		@State varchar(50) = null,
		@ZipCode varchar(20),
		@TaxRate decimal(4,2),
		@MenuID int Output 
	)

AS

DECLARE @iCount int

IF @MenuID = NULL
	BEGIN
		INSERT INTO dbo.Menus
                      (CategoryId, Name, Address, City, State, Zipcode, TaxRate)
		VALUES     (@CategoryID,@Name,@Address,@City,@State,@ZipCode,@TaxRate)
		
		SET @MenuID = SCOPE_IDENTITY()
		
	END
ELSE
	BEGIN
		SET @iCount = (SELECT Count(Id) FROM dbo.Menus WHERE Id = @MenuID)
		
		IF @iCount > 0
			BEGIN
				UPDATE    dbo.Menus
				SET              CategoryId = @CategoryID, Name = @Name, Address = @Address, City = @City, State = @State, Zipcode = @ZipCode, TaxRate = @TaxRate
					WHERE Id = @MenuID
			END	
		ELSE
			BEGIN
				INSERT INTO dbo.Menus
                      (CategoryId, Name, Address, City, State, Zipcode, TaxRate)
				VALUES     (@CategoryID,@Name,@Address,@City,@State,@ZipCode,@TaxRate)
				
				SET @MenuID = SCOPE_IDENTITY()
			END

	END

	RETURN


